package com.leyou.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.LyExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.Method;
import java.util.List;

@Service
@Transactional
public class BrandServiceImpl implements BrandService {

    private static final Logger logger = LoggerFactory.getLogger(Brand.class);

    @Autowired
    private BrandMapper brandMapper;

    @Override
    @Transactional
    public PageResult<BrandDTO> queryBrandByPage(Integer page, Integer rows, String key, String sortBy, Boolean desc) {
        PageHelper.startPage(page, rows);
//        System.out.println(page+"=="+rows+"=="+key+"=="+sortBy+"=="+desc);
        Example example = new Example(Brand.class);
        if(StringUtils.isNoneBlank(key)) {
            example.createCriteria().orLike("name", "%" + key + "%")
                    .orEqualTo("letter", key.toUpperCase());
        }
        if (StringUtils.isNoneBlank(sortBy)) {
            String orderByClause = sortBy + (desc ? " DESC" : " ASC");
            example.setOrderByClause(orderByClause);
        }
        List<Brand> brands = brandMapper.selectByExample(example);
        // 判断是否为空
        if(CollectionUtils.isEmpty(brands)){
            throw new LyException(LyExceptionEnum.BRAND_NOT_FOUND);
        }
        PageInfo<Brand> info = new PageInfo<>(brands);
        List<BrandDTO> brandDTOS = BeanHelper.copyWithCollection(brands, BrandDTO.class);
        return new PageResult<>(info.getTotal(), brandDTOS);
    }

    @Override
    @Transactional
    public void saveBrand(BrandDTO brandDTO, List<Long> ids) {
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        brand.setId(null);
        // 首先查询是否存在相同的品牌
        List<Brand> selectBrand = brandMapper.select(brand);
        if (selectBrand.size() != 0) {
            throw new LyException(LyExceptionEnum.INSERT_OPERATION_FAIL);
        }
        int i = brandMapper.insertSelective(brand);
        if (i != 1) {
            throw new LyException(LyExceptionEnum.INSERT_OPERATION_FAIL);
        }
        i = brandMapper.insertCategoryBrand(brand.getId(), ids);
        if(i != ids.size()){
            // 新增失败，抛出异常
            throw new LyException(LyExceptionEnum.INSERT_OPERATION_FAIL);
        }

    }


    @Override
    @Transactional
    public void updateBrand(BrandDTO brandDTO, List<Long> ids) {
        //修改品牌
        // 1. 更新brand表中的数据
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        int i = brandMapper.updateByPrimaryKeySelective(brand);
        if (i != 1) {
            throw new LyException(LyExceptionEnum.INSERT_OPERATION_FAIL);
        }
        // 2. 删除中间表的数据
        int count = brandMapper.deleteCategoryBrand(brand.getId());
        // 3. 新增中间表数据
        count = brandMapper.insertCategoryBrand(brand.getId(), ids);
        if (count != ids.size()) {
            throw new LyException(LyExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }
}
